package com.chuji.crmcj.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.bumptech.glide.Glide;
import com.chuji.crmcj.R;
import com.chuji.crmcj.act.HeadImageActivity;
import com.chuji.crmcj.act.MyTDCodeActivity;
import com.chuji.crmcj.act.SetActivity;
import com.chuji.crmcj.bean.MySelfInfo;
import com.chuji.crmcj.utils.Jump;
import com.chuji.crmcj.utils.SPUtils;
import com.chuji.crmcj.utils.UIUtils;
import com.chuji.crmcj.utils.UrlUtils;
import com.chuji.crmcj.utils.VolleyUtil;
import com.chuji.crmcj.view.CircleImageView;

import java.util.ArrayList;
import java.util.List;

public class MineFragment extends BaseFragment {
    /*用户头像*/
    private CircleImageView iv_head_iamge;
    /*用户姓名*/
    private TextView mTv_user_name;
    /*用户职位*/
    private TextView mTv_post;
    /*用户手机号码*/
    private TextView mTv_phoneNumber;
    /*我的二维码*/
    private TextView tv_my_two_dimens_code;
    /*设置*/
    private RelativeLayout RLSet;
    /*我的二维码*/
    private RelativeLayout RL_code;
    private Dialog dialog;
    /*用户头像网络数据存储*/
    List<MySelfInfo.ApiParamObjEntity> mySelfData;
    /*用户头像网络数据*/
    private MySelfInfo mySelfInfo;
    // sd路径
    private static String path = "/sdcard/myHead/";
    /*本地头像地址*/
    public String filePath = Environment.getExternalStorageDirectory() + "/myHead/head.jpg";
    /* 请求码 */
    private static final int IMAGE_REQUEST_CODE = 0;
    private static final int CAMERA_REQUEST_CODE = 1;
    private static final int RESULT_REQUEST_CODE = 2;
    //屏幕宽度
    private float scaleWidth;
    //屏幕高度
    private float scaleHeight;
    private Bitmap bp = null;
    int h;
    boolean num = false;
    //用户ID
    String SalesID;
    //意图
    Intent intent;

    @Override
    public void onStart() {
        super.onStart();
        //从服务端获取头像
        initDa();
    }

    @Override
    public void onResume() {
        super.onResume();
        initDa();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        //初始化组件
        tv_my_two_dimens_code = (TextView) view.findViewById(R.id.tv_my_two_dimens_code);
        iv_head_iamge = (CircleImageView) view.findViewById(R.id.iv_head_iamge);
        mTv_user_name = (TextView) view.findViewById(R.id.tv_user_name);
        mTv_post = (TextView) view.findViewById(R.id.tv_post);
        mTv_phoneNumber = (TextView) view.findViewById(R.id.tv_phoneNumber);
        RLSet = (RelativeLayout) view.findViewById(R.id.RLSet);
        RL_code = (RelativeLayout) view.findViewById(R.id.RL_code);
        /*跳转到我的二维码界面*/
        RL_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Jump.toNextActivity(getContext(), MyTDCodeActivity.class);
            }
        });
        /**
         * 跳转到设置界面
         */
        RLSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Jump.toNextActivity(getContext(), SetActivity.class);
            }
        });
        return view;
    }

    private void initDa() {
        //获取我的信息
        getMyMySelf();
    }

    private void getMyMySelf() {
        //第一次
        if (mySelfData == null) {
            mySelfData = new ArrayList<MySelfInfo.ApiParamObjEntity>();
        }
        requestData();
    }

    private void requestData() {
        //初始化Volley
        SalesID = SPUtils.getString(UIUtils.getContext(), "UserID", "");
        String strURL = String.format(UrlUtils.GET_MYINFO + "&SalesID=" + SalesID);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, strURL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                mySelfInfo = JSON.parseObject(response, MySelfInfo.class);
                mySelfData.addAll(mySelfInfo.getApiParamObj());
                UIUtils.runInMainThread(new Runnable() {
                    @Override
                    public void run() {
                        if (mySelfData.size() != 0) {
                            if (mySelfData.get(0).getAvatar() != null) {
                                Glide.with(UIUtils.getContext())
                                        .load(UrlUtils.ImageIP + mySelfData.get(0).getAvatar())
                                        .into(iv_head_iamge);
                                        /*上传头像*/
                                iv_head_iamge.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        Intent intent = new Intent(UIUtils.getContext(), HeadImageActivity.class);
                                        intent.putExtra("avatar", mySelfData.get(0).getAvatar());
                                        startActivity(intent);
                                    }
                                });
                            } else {
                                /*上传头像*/
                                iv_head_iamge.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        UIUtils.startActivity(new Intent(UIUtils.getContext(), HeadImageActivity.class));
                                    }
                                });
                                iv_head_iamge.setImageResource(R.drawable.weishetouxiang);
                            }

                            mTv_user_name.setText(mySelfData.get(0).getUserName());
                            mTv_post.setText(mySelfData.get(0).getPositionName());
//                            if(mySelfData.size()==11){
//                                mTv_phoneNumber.setText(NumberUtils.parsePhoneNumber(mySelfData.get(0).getMobilePhone()));
//                            }else{
                            mTv_phoneNumber.setText(mySelfData.get(0).getMobilePhone());
//                            }
                        } else {
                            UIUtils.showToastSafe("暂无数据");
                        }
                    }
                });
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                System.out.println("失败" + error);
                UIUtils.showToastSafe("请检查网络");
            }
        });
        // Add the request to the RequestQueue.
        VolleyUtil.getRequestQueue().add(stringRequest);
    }

    @Override
    public void initData() {

    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return null;
    }
}